package me.turingrobotapi.mcmod.turingapi.mcmod.turingapi.events;

import com.alibaba.fastjson.JSONObject;
import me.turingrobotapi.mcmod.turingapi.mcmod.turingapi.mcmod;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class Hello implements CommandExecutor {
    Plugin plugin = mcmod.getPlugin(mcmod.class);
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
        Player player=(Player) sender;
        if (args.length == 1)
        {
            String message = args[0];
            String useingapikey = plugin.getConfig().getString("apikey");
            String INFO = null;
            try {
                INFO = URLEncoder.encode(message, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String getURL = "http://www.tuling123.com/openapi/api?key=" + useingapikey + "&info=" + INFO;
            URL getUrl = null;
            try {
                getUrl = new URL(getURL);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            HttpURLConnection connection = null;
            try {
                connection = (HttpURLConnection) getUrl.openConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                connection.connect();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 取得输入流，并使用Reader读取
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            StringBuffer sb = new StringBuffer();
            String line = "";
            while (true) {
                try {
                    if (!((line = reader.readLine()) != null)) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                sb.append(line);
            }
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 断开连接
            connection.disconnect();
            JSONObject text = JSONObject.parseObject(String.valueOf(sb));
            System.out.println("【图灵机器人】 玩家名称：" + player + "，返回答复：" + text.getString("text"));
            player.sendMessage(ChatColor.BLUE + "[ Robot ] " + text.getString("text"));
            System.out.println("【图灵机器人】 成功返回答复");
        }else{
            player.sendMessage(ChatColor.RED + "语法错误");
            player.sendMessage(ChatColor.RED + "用法：/robot <Chat text>");
        }
        return false;
    }

}
